const mongodbClient = require("mongodb").MongoClient;
const Config = require("./config");

module.exports = {
    db: null,
    connect: function(dbName) {
        const that = this;
        if (!dbName) throw new Error("数据库名称不能为空");
        mongodbClient.connect(Config.url, function(err, db) {
            if (err) throw err;
            that.db = db.db(dbName);
            that.createCollection("geojson");
            return that.db;
        })
    },
    /**
     * 创建集合
     * @param {String} collectionName 
     */
    createCollection: function(collectionName) {
        const that = this;
        const db = that.db;
        db.createCollection(collectionName, function(err, res) {
            if (err) throw err;
            console.log("创建集合!");
        });
    },
    /**
     * 创建索引
     * @param {String} indexName 
     */
    createIndex: function(db, indexName) {
        const that = this;
        const db = that.db;

    },

    getDB(db) {
        return that.db || db;
    }
}